﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Web_Oto.Bases;
using Web_Oto.Data.Dto;

namespace Web_Oto.Data
{
    public class BannerDal : dalObject
    {
        #region constructors

        public BannerDal(string connectionstring, string connectiontype)
            : base(connectionstring, connectiontype)
        {

        }

        public BannerDal(IDbConnection sqlConnection, string connectiontype, SqlTransaction dbTransaction)
            : base(sqlConnection, connectiontype, dbTransaction)
        {

        }

        #endregion

        #region methods

        public IList<BannerDto> GetAllBanners()
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var i = 1;
                var bannerDtos = new List<BannerDto>();
                var banners = db.Banners.OrderBy(p => p.Id).ToList();
                foreach (var banner in banners)
                {
                    var bannerDto = new BannerDto
                                        {
                                            Id = banner.Id,
                                            STT = i,
                                            Image = banner.Image,
                                            Url = banner.Url,
                                            Intro = banner.Intro
                                        };
                    i++;
                    bannerDtos.Add(bannerDto);
                }

                return bannerDtos;
            }
        }

        public Banner GetBannerById(int id)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.Banners.FirstOrDefault(c => c.Id == id);
            }
        }

        public int Delete(int id)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var result = 1;
                try
                {
                    var banner = db.Banners.FirstOrDefault(p => p.Id == id);
                    if (banner != null)
                    {
                        db.Banners.DeleteOnSubmit(banner);
                        db.SubmitChanges();
                    }
                }
                catch (Exception)
                {
                    result = -1;
                }
                return result;
            }
        }

        public int Update(Banner banner)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var result = 1;
                try
                {
                    var news = db.Banners.FirstOrDefault(p => p.Id == banner.Id);
                    if (news != null)
                    {
                        news.Intro = banner.Intro;
                        if (!string.IsNullOrEmpty(banner.Image))
                            news.Image = banner.Image;
                        news.Url = banner.Url;
                        db.SubmitChanges();
                    }
                }
                catch (Exception)
                {
                    result = -1;
                }
                return result;
            }
        }

        public int Insert(Banner banner)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var result = 1;
                try
                {
                    db.Banners.InsertOnSubmit(banner);
                    db.SubmitChanges();
                }
                catch (Exception)
                {
                    result = -1;
                }
                return result;
            }
        }

        public string GetFilePathById(int id)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var news = db.Banners.FirstOrDefault(c => c.Id == id);
                if (news != null)
                    return news.Image;
            }

            return string.Empty;
        }

        #endregion
    }
}
